package com.zealcomm.ivcscustomer.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zco.util.ConvertUtil
import com.zco.util.StringUtil
import com.zco.util.ToastUtil
import com.zco.base.annotations.CommandStatus
import com.zco.base.entity.Command
import com.zco.base.entity.CommandViewBaseBean
import com.zco.base.entity.SessionMessage
import com.zco.ccs.entity.MessageSummaryBean
import com.zealcomm.commonui.base.ViewBindingFragment
import com.zealcomm.commonui.widget.RecyclerViewDivider
import com.zco.ivcs.customer.IvcsCustomerCallback
import com.zealcomm.ivcscustomer.app.IvcsApplication
import com.zealcomm.ivcscustomer.databinding.FragmentMessageSummaryBinding
import com.zealcomm.ivcscustomer.databinding.ItemMessageSummaryBinding
import com.zealcomm.ivcscustomer.service.MainActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class MessageSummaryFragment(messageSummarySessionMessage: SessionMessage<Command<CommandViewBaseBean<MutableList<MessageSummaryBean>>>>) :
    ViewBindingFragment<FragmentMessageSummaryBinding>() {
    private val messageSummarySessionMessage = messageSummarySessionMessage
    private val data = mutableListOf<MessageSummaryBean>()
    init {
        messageSummarySessionMessage?.data?.data?.data?.let {
            data.addAll(it)
        }
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.rvMessageSummary?.addItemDecoration(RecyclerViewDivider(
            requireContext(), LinearLayoutManager.VERTICAL,
            ConvertUtil.dp2px(20), Color.TRANSPARENT, true
        ))
        binding?.rvMessageSummary?.adapter = object : RecyclerView.Adapter<MyViewHolder>(){
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                return MyViewHolder(ItemMessageSummaryBinding.inflate(layoutInflater , parent , false))
            }

            override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                holder.binding.tvTitle.text = StringUtil.getValue(data[position].title , "--")
                holder.binding.tvDuration.text = "${data[position].duration / 1000}"
                holder.binding.tvAsk.text = StringUtil.getValue(data[position].ask , "--")
                holder.binding.tvAnswer.text = StringUtil.getValue(data[position].answer , "--")
            }

            override fun getItemCount(): Int {
                return data.size
            }

        }
        binding?.btnCancel?.setOnClickListener {
            response(CommandStatus.REJECTED)
        }
        binding?.btnSure?.setOnClickListener {
            response(CommandStatus.DONE)
        }
    }

    private fun response(@CommandStatus status:String){
        messageSummarySessionMessage.data.status = status
        IvcsApplication.ivcsCustomer!!.sendPeerResponse(null , messageSummarySessionMessage.data , object : IvcsCustomerCallback{
            override fun onSuccess() {
                GlobalScope.launch(Dispatchers.Main) {
                    (holdingActivity as MainActivity).removeFragment()
                }
            }

            override fun onFailed(code: Int, error: String?) {
                GlobalScope.launch(Dispatchers.Main) {
                    ToastUtil.showMiddleToast(error)
                }
            }
        })
    }

    private class MyViewHolder(binding:ItemMessageSummaryBinding):RecyclerView.ViewHolder(binding.root){
        val binding = binding
    }
}